package com.brspace.qualitycontrol.controller.admin;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brspace.qualitycontrol.entity.*;
import com.brspace.qualitycontrol.entity.vo.CollertWritVo;
import com.brspace.qualitycontrol.entity.vo.EngineeringVo;
import com.brspace.qualitycontrol.entity.vo.MonthNumVo;
import com.brspace.qualitycontrol.entity.vo.PaginationVo;
import com.brspace.qualitycontrol.mapper.CollectWritMapper;
import com.brspace.qualitycontrol.mapper.ProcessNodeMapper;
import com.brspace.qualitycontrol.service.CollectWritService;
import com.brspace.qualitycontrol.service.UserService;
import com.brspace.qualitycontrol.service.WritUserService;
import com.brspace.qualitycontrol.utils.EmptyUtil;
import com.brspace.qualitycontrol.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 收文 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-07
 */
@Api(description = "收文")
@CrossOrigin
@RestController
@RequestMapping("/admin/collect-writ")
public class CollectWritController {
    @Resource
    private CollectWritService collectWritService;
    @Resource
    private CollectWritMapper collectWritMapper;
    @Resource
    private WritUserService writUserService;
    @Resource
    private UserService userService;
    @Resource
    private ProcessNodeMapper processNodeMapper;

    /**
     * 新建收文
     * @param collectWrit
     * @return
     */
    @ApiOperation("新建收文")
    @PostMapping("addColl")
    @Transactional
    public R addColl( CollectWrit collectWrit){
        collectWritService.saveInfo(collectWrit);
        return R.ok();

    }

    /**
     * 收文流程运转
     *@author Hugo
     *@date 2021-04-28 09:25
     **/
    @PostMapping("examine/{nextId}/{userId}")
    public R examine( CollectWrit collectWrit,@PathVariable String  nextId,@PathVariable String userId){
        collectWritService.examine(collectWrit,nextId,userId);
        return R.ok();
    }

    /**
     *显示代办事项
     *@author Hugo
     *@date 2021-04-28 14:57
     **/
    @PostMapping("pageUnCompleteList/{page}/{limit}/{userId}")
    public R pageUnCompleteList(@PathVariable int page,@PathVariable int limit,@PathVariable String userId){
        return R.ok().data("list",collectWritService.pageUnCompleteList(page,limit,userId)).data("total",collectWritService.pageUnCompleteList(page,limit,userId).size());
    }

    /**
     *显示已办事项
     *@author Hugo
     *@date 2021-04-28 16:04
     **/
    @PostMapping("pageCompleteList/{page}/{limit}/{userId}")
    public R pageCompleteList(@PathVariable int page,@PathVariable int limit,@PathVariable String userId){
        return R.ok().data("list",collectWritService.pageCompleteList(page,limit,userId)).data("total",collectWritService.pageCompleteList(page,limit,userId).size());
    }

    /**
     *显示全部事项
     *@author Hugo
     *@date 2021-04-26 16:13
     **/
    @PostMapping("getAll/{page}/{limit}/{userId}")
    public R getAll(@PathVariable int page,@PathVariable int limit,@PathVariable String userId){
        return R.ok().data("list",collectWritService.pageAllList(page,limit,userId)).data("total",collectWritService.pageAllList(page,limit,userId).size());

    }
    /**
     * 全部收文列表
     * @param page
     * @param limit
     * @param collertWritVo
     * @return
     */
    @ApiOperation("全部收文列表")
    @PostMapping("list/{page}/{limit}")
    public R pageList(
            @ApiParam(value = "当前页码",required = true) @PathVariable Long page,
            @ApiParam(value = "每页多少数据", required = true) @PathVariable Long limit,
            @ApiParam("讲师列表查询条件") CollertWritVo collertWritVo){
        Page<CollectWrit> pageParam = new Page<>(page, limit);
        IPage<CollectWrit> pageModel = collectWritService.selectPage(pageParam, collertWritVo);
        List<CollectWrit> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "分页查询当前用户未阅收文列表", notes = "分页查询当前用户未阅收文列表")
    @PostMapping("getNoPullList")
    public R getNoPullList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize") Integer pageSize){
        //当前登录的用户Id
        String userId = "1376058930834300930";
        PaginationVo<CollectWrit> page = collectWritService.getPullList(pageNum,pageSize,userId,0);
        return R.ok().data("data",page);
    }

    @ApiOperation(value = "分页查询当前用户已阅收文列表", notes = "分页查询当前用户已阅收文列表")
    @PostMapping("getYesPullList")
    public R getYesPullList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize") Integer pageSize){
        //当前登录的用户Id
        String userId = "1376058930834300930";
        PaginationVo<CollectWrit> page = collectWritService.getPullList(pageNum,pageSize,userId,1);
        return R.ok().data("data",page);
    }

    @ApiOperation(value = "根据id查询收文详情", notes = "根据id查询收文详情")
    @PostMapping("selectPullDraftById/{userId}")
    public R findInStockList(CollectWrit collectWrit){
        CollectWrit byId = collectWritService.getById(collectWrit.getId());
        int s = 0;
        if (collectWritMapper.selectById(collectWrit).getNodeId() != 0 ){
            s = processNodeMapper.selectById(collectWritMapper.selectById(collectWrit).getNodeId()).getSortNum();
        }
        return R.ok().data("data",byId).data("number",s);
    }

    @ApiOperation(value = "每月统计收文数量", notes = "每月统计收文数量")
    @PostMapping("statisticsByMonth")
    public R statisticsByMonth(){
        List<MonthNumVo> list = collectWritService.statisticsByMonth();
        return R.ok().data("data",list);
    }

    @ApiOperation(value = "计算今年收文数量和去年收文数量", notes = "计算今年收文数量和去年收文数量")
    @PostMapping("nowAndLastNum")
    public R nowAndLastNum(){
        return collectWritService.getNowAndLastNum();
    }

    @ApiOperation(value = "收文类型统计列表", notes = "收文类型统计列表")
    @PostMapping("typeNumList")
    public R typeNumList(){
        return collectWritService.getTypeNumList();
    }

}

